package qyWechat

import (
	"encoding/json"
	"errors"
	"io/ioutil"
	"net/http"
	"strings"
)

/* 群机器人 */

type Robot struct {
}

func NewRobot() *Robot {
	return &Robot{}
}

func (r *Robot) SendString(urlStr, context string) (err error) {
	res, err := http.Post(urlStr, "application/json;charset=UTF-8", strings.NewReader(context))
	if err != nil {
		return
	}
	defer res.Body.Close()
	b, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return
	}
	var m map[string]interface{}
	err = json.Unmarshal(b, &m)
	if err != nil {
		return
	}
	if m["errcode"].(float64) != 0 {
		return errors.New(m["errmsg"].(string))
	}
	return
}
